package com.itfetter.cart.controller;




import com.itfetter.cart.domain.dto.CartFormDTO;
import com.itfetter.cart.domain.po.Cart;
import com.itfetter.cart.domain.vo.CartVO;
import com.itfetter.cart.service.ICartService;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 购物车相关接口
 */
@RestController
@RequestMapping("/carts")
@RequiredArgsConstructor
public class CartController {
    private final ICartService cartService;

    /**
     * 添加商品到购物车
     * @param cartFormDTO
     */
    @PostMapping
    public void addItem2Cart(@Valid @RequestBody CartFormDTO cartFormDTO){
        cartService.addItem2Cart(cartFormDTO);
    }

    /**
     * 更新购物车数据
     * @param cart
     */
    @PutMapping
    public void updateCart(@RequestBody Cart cart){
        cartService.updateById(cart);
    }

    /**
     * 删除购物车商品
     * @param id
     */
    @DeleteMapping("{id}")
    public void deleteCartItem(@Param ("购物车条目id")@PathVariable("id") Long id){
        cartService.removeById(id);
    }

    /**
     * 查询购物车列表
     * @return
     */
    @GetMapping
    public List<CartVO> queryMyCarts(){
        return cartService.queryMyCarts();
    }

    /**
     * 批量删除购物车中的商品
     * @param ids 购物车条目集合
     */
    @DeleteMapping
    public void deleteCartItemByIds(@RequestParam("ids") List<Long> ids){
        cartService.removeByItemIds(ids);
    }
}
